<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GeoGebra 2D 绘图工具</title>
    <!-- 引入 Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <!-- 引入 Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;700&display=swap" rel="stylesheet">
    <style>
        /* CSS 独立封装 */
        body {
            font-family: 'Noto Sans SC', sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            height: 100vh;
        }
        header {
            background-color: #f8f9fa;
            padding: 1rem;
            text-align: center;
        }
        .container {
            display: flex;
            flex: 1;
        }
        #geogebra-container {
            width: 600px;
            height: 500px;
            border: 1px solid #ccc;
        }
        .control-panel {
            padding: 1rem;
            width: 300px;
            background-color: #f8f9fa;
            border-left: 1px solid #ccc;
        }
        .control-panel button {
            margin: 0.5rem 0;
            padding: 0.5rem 1rem;
            cursor: pointer;
        }
        .control-panel input {
            margin: 0.5rem 0;
            padding: 0.5rem;
            width: 100%;
        }
    </style>
</head>
<body>
    <header>
        <h1>GeoGebra 2D 绘图工具</h1>
    </header>
    <div class="container">
        <div id="geogebra-container"></div>
        <div class="control-panel">
            <button id="draw-triangle">绘制三角形</button>
            <input type="text" id="triangle-side" placeholder="输入三角形边长">
        </div>
    </div>

    <!-- 引入 GeoGebra 官方 CDN -->
    <script src="https://cdn.geogebra.org/apps/deployggb.js"></script>
    <script>
        // GeoGebra 初始化参数
        const parameters = {
            "appName": "classic",
            "width": "600",
            "height": "500",
            "showToolBar": true,
            "showAlgebraInput": false,
            "showMenuBar": false,
            "allowStyleBar": true,
            "enable3d": false,
            "appletOnLoad": function(api) {
                // 初始化形状函数
                initializeShape(api);
            },
            "language": "zh-CN"
        };

        // 创建 GeoGebra Applet 实例
        const applet = new GGBApplet(parameters, true);

        // 页面加载完成后注入 GeoGebra 到指定容器
        window.onload = function() {
            applet.inject('geogebra-container');
        };

        // 初始化形状函数
        function initializeShape(api) {
            document.getElementById('draw-triangle').addEventListener('click', () => {
                const sideLength = document.getElementById('triangle-side').value;
                if (sideLength) {
                    // 使用 evalCommand 绘制三角形
                    api.evalCommand(`Polygon[(0,0), (${sideLength},0), (${sideLength}/2, ${Math.sqrt(3)/2 * sideLength})]`);
                } else {
                    alert('请输入三角形边长');
                }
            });
        }
    </script>
</body>
</html>